cmake_minimum_required( VERSION 3.14.4)
project(usb_cam)

set(CATKIN_PACKAGE_SHARE_DESTINATION "share")
set(CATKIN_PACKAGE_BIN_DESTINATION "bin")
set(CATKIN_PACKAGE_LIB_DESTINATION "lib")
set(CATKIN_PACKAGE_INCLUDE_DESTINATION "include")
## Find catkin macros and libraries
## if COMPONENTS list like find_package(ament_cmake_ros REQUIRED) COMPONENTS xyz)
## is used, also find other catkin packages
find_package(ament_cmake_ros REQUIRED) 
find_package(image_transport  REQUIRED) 
find_package(std_msgs  REQUIRED) 
find_package(sensor_msgs  REQUIRED) 
find_package(OpenCV  REQUIRED) 
find_package(cv_bridge  REQUIRED) 
find_package(rclcpp  REQUIRED) 
## pkg-config libraries
find_package(PkgConfig REQUIRED)
pkg_search_module(avcodec REQUIRED libavcodec )
pkg_search_module(avutil REQUIRED libavutil )
pkg_search_module(swscale REQUIRED libswscale )
###################################################
## Declare things to be passed to other projects ##
###################################################

## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
#catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES ${PROJECT_NAME}
#)

###########
## Build ##
###########

include_directories(include
  ${avcodec_INCLUDE_DIRS}
  ${avutil_INCLUDE_DIRS}
  ${swscale_INCLUDE_DIRS}
  ${OpenCV_INCLUDE_DIRS}
  ${cv_bridge_INCLUDE_DIRS}
  /opt/ros/$ENV{ROS_DISTRO}/include/cv_bridge/
  /opt/ros/$ENV{ROS_DISTRO}/include/std_msgs/
  /opt/ros/$ENV{ROS_DISTRO}/include/statistics_msgs/
  /opt/ros/$ENV{ROS_DISTRO}/include/sensor_msgs/
  /opt/ros/$ENV{ROS_DISTRO}/include/service_msgs/
  /opt/ros/$ENV{ROS_DISTRO}/include/rosidl_runtime_cpp/
  /opt/ros/$ENV{ROS_DISTRO}/include/rosidl_runtime_c/
  /opt/ros/$ENV{ROS_DISTRO}/include/rosidl_typesupport_interface/
  /opt/ros/$ENV{ROS_DISTRO}/include/rosidl_dynamic_typesupport/
  /opt/ros/$ENV{ROS_DISTRO}/include/rosidl_typesupport_introspection_cpp/
  /opt/ros/$ENV{ROS_DISTRO}/include/builtin_interfaces/
  /opt/ros/$ENV{ROS_DISTRO}/include/rcutils/
  /opt/ros/$ENV{ROS_DISTRO}/include/rcpputils/
  /opt/ros/$ENV{ROS_DISTRO}/include/rmw/
  /opt/ros/$ENV{ROS_DISTRO}/include/rcl/
  /opt/ros/$ENV{ROS_DISTRO}/include/rclcpp/
  /opt/ros/$ENV{ROS_DISTRO}/include/rcl_interfaces/
  /opt/ros/$ENV{ROS_DISTRO}/include/rcl_yaml_param_parser/
  /opt/ros/$ENV{ROS_DISTRO}/include/type_description_interfaces/
  /opt/ros/$ENV{ROS_DISTRO}/include/libstatistics_collector/
  /opt/ros/$ENV{ROS_DISTRO}/include/tracetools/
)

## Build the USB camera library
add_library(${PROJECT_NAME} src/usb_cam.cpp)
target_link_libraries(${PROJECT_NAME}
  -lavcodec 
  ${rclcpp_LIBRARIES}
  ${std_msgs_LIBRARIES}
  ${sensor_msgs_LIBRARIES}
)

## Declare a cpp executable
add_executable(${PROJECT_NAME}_node nodes/usb_cam_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
  ${PROJECT_NAME}
  -lavcodec 
  ${OpenCV_LIBRARIES}
  ${rclcpp_LIBRARIES}
)

#############
## Install ##
#############

## Mark executables and/or libraries for installation
install(TARGETS ${PROJECT_NAME}_node ${PROJECT_NAME}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)

## Copy launch files
install(DIRECTORY launch/
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
  FILES_MATCHING PATTERN "*.launch"
)

install(DIRECTORY include/${PROJECT_NAME}/
   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
   FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
)
# Add this to the bottom of your CMakeLists.txt
ament_package()
